package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.Constants;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @description 说明
 * @package com.zzyl.service.impl
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Resource
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 护理项目分页查询
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> page(String name, Integer pageNum, Integer pageSize, Integer status) {
        // 1、 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 2、查询
        Page<NursingProject> pageInfo = nursingProjectMapper.selectPage(name, status);
        // 3、封装结果返回  PageResponse.of????
        //pageInfo.getTotal();
        //pageInfo.getResult();
        // 构建分页结果对象,使用指定的类型把NursingProject转成vo类型


        // 返回类型PageResponse<NursingProjectVo>
       /* PageResponse<NursingProjectVo> vo = new PageResponse<NursingProjectVo>();
        vo.setPage(pageInfo.getPageNum());
        vo.setPages(Long.valueOf(pageInfo.getPages()));
        vo.setTotal(pageInfo.getTotal());
        vo.setPageSize(pageInfo.getPageSize());

        // 分页结果集
        List<NursingProject> list = pageInfo.getResult();
        // 转换
        List<NursingProjectVo> npmList = list.stream()
            .map(np -> BeanUtil.toBean(np, NursingProjectVo.class))
            .collect(Collectors.toList());
        vo.setRecords(npmList);
        return vo;*/

        return PageResponse.of(pageInfo, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     *
     * @param dto
     */
    @Override
    public void add(NursingProjectDto dto) {
        // 转成entity
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        // 设置属性, 有拦截器来实现统一处理
        // 调用mapper新增
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    /**
     * 编辑护理项目
     *
     * @param dto
     */
    @Override
    public void updateById(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProjectMapper.updateById(nursingProject);
    }

    /**
     * 启禁用护理项目
     *
     * @param id
     * @param status
     */
    @Override
    public void enable(Long id, Integer status) {
        nursingProjectMapper.enable(id,status);
    }

    /**
     * 通过id删除护理项目
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
               if(ObjectUtil.isNotEmpty(nursingProject) &&
            Constants.NURSING_PROJECT_STATUS_ENABLE.equals(nursingProject.getStatus())){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProject> findAll() {
        return nursingProjectMapper.findAll();
    }
}
